package com.zx.chatroom.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zx.chatroom.po.GroupChat;
import com.zx.chatroom.po.GroupMembers;
import com.zx.chatroom.util.CONST;
import com.zx.framework.core.dao.IBaseDAO;

@Component("groupChatService")
@Transactional
public class GroupChatService {

	private static final Logger LOGGER = Logger.getLogger(GroupChatService.class);
	@Resource
	private IBaseDAO baseDAO;
	
	/**
	 * 获取用户的群组
	 * @param userid
	 * @return
	 */
	public List<GroupChat> getGroupChatsByUserid(String userid){
		String hql = "from GroupChat gc where gc.status = ? and exists(from GroupMembers gm where gm.id.groupid = gc.groupid and gm.id.userid = ?)";
		return baseDAO.queryHQL(hql, CONST.OBJECT_USED+"", userid);
	}
	/**
	 * 取得群组成员ID
	 * @return
	 */
	public List<GroupMembers> getUserByGroupId(String groupId){
		String hql = "from GroupMembers gm where gm.id.groupid = ?";
		return baseDAO.queryHQL(hql, groupId);
	}
	/**
	 * 取得该用户所在群组ID
	 * @param userId
	 * @return
	 */
	public List<GroupMembers> getGroupByUserId(String userId){
		String hql = "from GroupMembers gm where gm.id.userid = ?";
		return baseDAO.queryHQL(hql, userId);
	}
	
	public GroupChat getGroupChatById(String groupId){
		return baseDAO.findById(groupId, GroupChat.class);
	}
}
